//
//  HMStatusCell.swift
//  新浪微博
//
//  Created by 饶鹏伟 on 16/4/11.
//  Copyright © 2016年 饶鹏伟. All rights reserved.
//

import UIKit
import SnapKit

class HMStatusCell: UITableViewCell {

   //懒加载原创微博子控件
    private lazy var originalView: HMStatusOriginalView=HMStatusOriginalView()
    //懒加载转发微薄的子控件
    private lazy var statusRetweetedView: HMStatusRetweetedView=HMStatusRetweetedView()
   //懒加载底部工具条的子控件
    private lazy var statusToolBar: HMStatusToolBar=HMStatusToolBar()
    
    
    //工具条的顶部的约束条件
    var tooBarTopCons: Constraint?
    
   //定义一个模型属性statusViewModel 获取里面的cell模型
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //给子控件赋值 传入一个模型 然后子控件从模型中获取数据
            originalView.statusViewModel = statusViewModel
            
            statusToolBar.statusViewModel = statusViewModel
            
            
            //判断是否是转发微博 修改toolBar的顶部约束
            //防止cell复用  snp_updateConstraints: 如果约束存在就更新,约束不存在就创建
            //在更新约束之前先 卸载toolbar顶部的约束  先记录顶部约束
            self.tooBarTopCons?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil{
                //有转发微博 
                //修改约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.tooBarTopCons=make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                })
               //显示转发微博
                statusRetweetedView.hidden=false
               //设置数据源
                statusRetweetedView.statusViewModel=statusViewModel
            }else{
                
                //是原创微博 没有转发微博
                //修改约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.tooBarTopCons=make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
                
                //隐藏转发微博
                statusRetweetedView.hidden=true
                
                //设置数据源
                statusRetweetedView.statusViewModel=nil
                
            }

        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: .Default, reuseIdentifier: reuseIdentifier)
        
        //设置cell的显示
        setupUI()
        
        //设置cell选中的背景色
        selectionStyle = .None
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    //设置cell显示
    private func setupUI(){
        
        //backgroundColor = randomColor()
        //添加子视图
        contentView.addSubview(originalView)
        contentView.addSubview(statusRetweetedView)
        contentView.addSubview(statusToolBar)
        
        //设置原创微博视图的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(contentView)
            
        }
        
        //设置转发微薄的约束
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
           // make.height.equalTo(80)
        
        }
        
        //设置底部工具条的视图约束
        statusToolBar.snp_makeConstraints { (make) -> Void in
            
           self.tooBarTopCons=make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
            
        }
        
        
       //添加contentView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            
           make.left.right.top.equalTo(self)
            
            make.bottom.equalTo(statusToolBar.snp_bottom)
            
        }
        
        
    }
    
}
